package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mes.bean.Equipment;
import com.efast.dmom.mes.bean.ProductionImplementationAccept;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.EquipSpareDao;
import com.efast.dmom.mes.dao.entity.StartWorkDao;
import com.efast.dmom.mes.datahandlers.entity.EquipmentDataHandler;
import com.efast.dmom.mes.entity.EquipSpareEntity;
import com.efast.dmom.mes.entity.StartWorkEntity;
import com.efast.dmom.mes.service.StartWorkService;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;

@Transactional
@Service
public class StartWorkServiceImpl implements StartWorkService {

	@Autowired
	private StartWorkDao startWorkDao;

	@Autowired
	private EquipSpareDao equipSpareDao;

	@Autowired
	private EquipmentDataHandler equipmentDataHandler;

	@Override
	public List<StartWorkEntity> getStartWorkByPartIdAndOperationId(ProductionImplementationAccept acceptBean) {
		List<StartWorkEntity> startWorkEntityList = startWorkDao.getStartWorkByPartIdAndOperationId(
				acceptBean.getPartId(), acceptBean.getOperationId(), acceptBean.getUser());
		return startWorkEntityList;
	}

	@Override
	public ResultBean StartWork(StartWorkEntity startWorkEntity) {
		ResultBean resultBean = new ResultBean(true, ' ');
		EquipSpareEntity byCutterCodeAndStatus = equipSpareDao
				.getByCutterCodeAndStatus(startWorkEntity.getCutterCode());
		if (byCutterCodeAndStatus != null) {
			if (byCutterCodeAndStatus.getUsedStatus() != 0) {
				throw new RuntimeException("刀具状态必须为未使用状态才能开工");
			}
			Equipment selectEquipSpareDaoJuInfoByDaoJuCode = equipmentDataHandler
					.selectEquipSpareDaoJuInfoByDaoJuCode(startWorkEntity.getCutterCode());
			if (selectEquipSpareDaoJuInfoByDaoJuCode.getInnerData().getUsedStatus() != 0) {
				throw new RuntimeException("刀具状态必须为未使用状态才能开工");
			}
			// 更新备件的状态
			equipSpareDao.updateStatusById(byCutterCodeAndStatus.getId(), 1);
			equipmentDataHandler
					.updateStatusById(selectEquipSpareDaoJuInfoByDaoJuCode.getInnerData().getSpareStorageId(), 1);
		}
		if (startWorkEntity.getEquipId() != 0) {
			List<StartWorkEntity> equipList = startWorkDao.getStartWorkListByEquipId(startWorkEntity.getEquipId());
			if (equipList != null && equipList.size() > 0) {
				throw new RuntimeException("该设备已在产品[" + equipList.get(0).getPartName() + "]、工序["
						+ equipList.get(0).getOperationName() + "]被用户[" + equipList.get(0).getStartWorkUser() + "]开工！");
			}
			if (startWorkEntity.getSpareId() != 0) {
				List<StartWorkEntity> spareList = startWorkDao.getStartWorkListBySpareId(startWorkEntity.getSpareId());
				if (byCutterCodeAndStatus != null) {
					if (spareList != null && spareList.size() > 0) {
						throw new RuntimeException("该刀具已在产品[" + equipList.get(0).getPartName() + "]、工序["
								+ equipList.get(0).getOperationName() + "]被用户[" + equipList.get(0).getStartWorkUser()
								+ "]开工！");
					}
				}
			}
		}
		// 通过备件编码查询备件数据
//		EquipSpareEntity bySpareCode = equipSpareDao.getBySpareCode(startWorkEntity.getSpareCode());
//		startWorkEntity.setSpareCode(bySpareCode.getCode());
//		startWorkEntity.setSpareId(bySpareCode.getId());
		// 保存数据
		startWorkEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
		startWorkEntity.setCreationTime(TimeUtil.getLocalDateTime());
		startWorkEntity.setCreator(SpringWebUtils.getRequestUser());
		startWorkEntity.setIsFlag(0);
		startWorkEntity.setStartWorkDate(TimeUtil.getLocalDateTime());
		startWorkEntity.setRowStatus(0);
		int saveData = startWorkDao.save(startWorkEntity);
		if (saveData > 0) {
			List<StartWorkEntity> startWorkEntityList = startWorkDao.getStartWorkByPartIdAndOperationId(
					startWorkEntity.getPartId(), startWorkEntity.getOperationId(), startWorkEntity.getStartWorkUser());
			resultBean.setObject(startWorkEntityList);
			resultBean.setSuccess(true);
			resultBean.setMessage("开工成功！");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("开工失败！");
		}
		return resultBean;
	}

	@Override
	public StartWorkEntity getStartWork(long partId, long operationId) {
		StartWorkEntity startWorkEntity = startWorkDao.getStartWork(partId, operationId);
		return startWorkEntity;
	}

	@Override
	public ResultBean afreshStartWorking(StartWorkEntity startWorkEntity) {
		ResultBean resultBean = new ResultBean(true, " ");
		// 判断刀具状态是否为已使用
//		EquipSpareEntity byCutterCodeAndStatus = equipSpareDao.getByCutterCodeAndStatus(startWorkEntity.getCutterCode());
//		if (byCutterCodeAndStatus.getUsedStatus() != 1) {
//			throw new ServiceException("刀具状态必须为已使用状态才能开工","刀具状态必须为已使用状态才能开工");
//		}
//		Equipment selectEquipSpareDaoJuInfoByDaoJuCode = equipmentDataHandler.selectEquipSpareDaoJuInfoByDaoJuCode(startWorkEntity.getCutterCode());
//		if (selectEquipSpareDaoJuInfoByDaoJuCode.getInnerData().getUsedStatus() != 1) {
//			throw new ServiceException("备件状态必须为已使用状态才能开工","备件状态必须为已使用状态才能开工");
//		}
		List<StartWorkEntity> startWorkList = startWorkDao.getStartWorkListByUser(startWorkEntity);
		if (startWorkList == null || startWorkList.size() == 0) {
			throw new RuntimeException("无重新开工的设备和刀具！");
		}
		startWorkEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		startWorkEntity.setLastModifier(SpringWebUtils.getRequestUser());
		startWorkEntity.setIsFlag(1);
		startWorkDao.afreshStartWorking(startWorkEntity);
		// 更新备件的状态
		for (StartWorkEntity data : startWorkList) {
			equipSpareDao.updateStatusById(data.getSpareId(), 0);
			equipmentDataHandler.updateStatusBySpareId(data.getSpareId(), 0);
		}
		resultBean.setSuccess(true);
		resultBean.setMessage("重新开工成功！");
		return resultBean;
	}

}
